/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ufg.inf.es.agenda.Servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ufg.inf.es.agenda.Model.Endereco;
import ufg.inf.es.agenda.Model.Pessoa;
import ufg.inf.es.agenda.Model.Telefone;
import ufg.inf.es.agenda.presistencia.impl.GenericDAO;

/**
 *
 * @author wilker
 */
@WebServlet(name = "Editar", urlPatterns = {"/Editar"})
public class Editar extends HttpServlet {

    private String corpo = "<!DOCTYPE html>"
            + "<html lang='pt-BR'>"
            + "    <head>"
            + "        <meta charset='utf-8'>"
            + "        <title>Smart-Agenda</title>"
            + "        <link rel='shortcut icon' href='img/a.png'>"
            + "        <link href='css/bootstrap.css' rel='stylesheet'>"
            + "        <style type='text/css'>"
            + "            body {"
            + "                padding-top: 60px;"
            + "                padding-bottom: 40px;"
            + "            }"
            + "            .sidebar-nav {"
            + "                padding: 9px 0;"
            + "            }"
            + "        </style>"
            + "        <link href='css/bootstrap-responsive.css' rel='stylesheet'>"
            + "    </head>"
            + "    <body>"
            + "        <div class='navbar navbar-fixed-top'>"
            + "            <div class='navbar-inner'>"
            + "                <div class='container-fluid'>"
            + "                    <a class='btn btn-navbar' data-toggle='collapse' data-target='.nav-collapse'>"
            + "                        <span class='icon-bar'></span>"
            + "                        <span class='icon-bar'></span>"
            + "                        <span class='icon-bar'></span>"
            + "                    </a>"
            + "                    <a class='brand' href='Consulta?nome='>Smart-Agenda</a>"
            + "                    <div class='nav-collapse'>"
            + "                        <p class='navbar-text pull-right'>Bem Vindo a nossa <a href='Consulta?nome='>smart-agenda</a></p>"
            + "                    </div>"
            + "                </div>"
            + "            </div>"
            + "            </div>"
            + "        <div class='container-fluid'>"
            + "        <div class='row-fluid'>"
            + "                <div class='span3'>"
            + "                    <div class='well sidebar-nav'>"
            + "                        <ul class='nav nav-list'>"
            + "                            <li class='nav-header'>Cadastro</li>"
            + "                            <li><a href='novo.html' >Cadastrar Contato</a></li>"
            + "                            <li class='nav-header'>Consulta</li>"
            + "                            <li class='active'><a href='Consulta?nome='>Contatos Geral</a></li>"
            + "                        </ul>"
            + "                    </div><!--/.well -->"
            + "                </div><!--/span-->"
            + "                <div class='span9'>"
            + "                    <div class='hero-unit' >";
    private String rodape = "     </div>"
            + "                </div>"
            + "            </div>"
            + "            <hr>"
            + "            <footer>"
            + "                <p align='center'>&copy; Wilker de Jesus Machado --- Matricula: 090230</p>"
            + "            </footer>"
            + "        </div>"
            + "    </body>"
            + "</html>";

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        String id = (request.getParameter("id"));
        List<Pessoa> pessoas = GenericDAO.busca(Pessoa.class, "id=" + id);
        Pessoa p = pessoas.get(0);
        String cidade = "", estado = "", rua = "", cmp = "", cep = "", tel1 = "", tel2 = "", setor = "";
        for (Endereco e : p.getEnderecos()) {
            estado = e.getEstado();
            cidade = e.getCidade();
            rua = e.getRua();
            cmp = e.getComplemento();
            setor = e.getSetor();
            cep = e.getCep();
        }
        List<Telefone> t = new ArrayList<Telefone>(p.getTelefones());
        tel1 = t.get(0).getNumero();
        tel2 = t.get(1).getNumero();
        PrintWriter out = response.getWriter();
        out.print(corpo);
        out.print("                        <form action='Cadastro' method='POST' class='form-horizontal'>"
                + "                         <input type='hidden' name='id' value='"+p.getId()+"' />"
                + "                            <fieldset>"
                + "                                <legend>Editar Contato na Agenda</legend>"
                + "                                <div class='control-group'>"
                + "                                    <label class='control-label' for='input01'>Nome: &nbsp;</label>"
                + "                                    <input type='text' name='nome' value='" + p.getNome() + "' class='input-xlarge' id='input01'>"
                + "                                </div>"
                + "                                <div class='control-group'>"
                + "                                    <label class='control-label' for='input02'>CPF: &nbsp;</label>"
                + "                                    <input type='text' name='cpf' value='" + p.getCpf() + "' class='input-medium' id='input02'>"
                + "                                </div>"
                + "                                <div class='control-group'>"
                + "                                    <label class='control-label' for='input03'>Nascimento: &nbsp;</label>"
                + "                                    <input type='text' name='dataNascimento' value='" + p.getDataNascimento().toString() + "' class='input-medium' id='input03'> dd/mm/aaaa"
                + "                                </div>"
                + "                                <div class='control-group'>"
                + "                                    <label class='control-label' for='input04'>Telefones: &nbsp;</label>"
                + "                                    <input type='text' name='telefone1' value='" + tel1 + "' class='input-medium' id='input04'>"
                + "                                    <input type='text' name='telefone2' value='" + tel2 + "' class='input-medium' id='input05'>"
                + "                                </div>"
                + "                                <div class='control-group'>"
                + "                                    <label class='control-label' for='input06'>Endereco: &nbsp;</label>"
                + "                                    <input type='text' name='endereco' value='" + rua + "' class='input-large' id='input06'>"
                + "                                </div>"
                + "                                <div class='control-group'>"
                + "                                    <label class='control-label' for='input07'>Complemento: &nbsp;</label>"
                + "                                    <input type='text' name='complemento' value='" + cmp + "' class='input-large' id='input07'>"
                + "                                    </div>"
                + "                                    <div class='control-group'>"
                + "                                    <label class='control-label' for='input08'>Setor: &nbsp;</label>"
                + "                                    <input type='text' name='setor' value='" + setor + "' class='input-large' id='input08'>"
                + "                                </div>"
                + "                                <div class='control-group'>"
                + "                                    <label class='control-label' for='input09'>Cidade: &nbsp;</label>"
                + "                                    <input type='text' name='cidade' value='" + cidade + "' class='input-medium' id='input09'>"
                + "                                </div>"
                + "                                <div class='control-group'>"
                + "                                    <label class='control-label' for='input10'>Estado: &nbsp;</label>"
                + "                                    <input type='text' name='estado' value='" + estado + "' class='input-medium' id='input10'>"
                + "                                </div>"
                + "                                <div class='control-group'>"
                + "                                    <label class='control-label' for='input11'>CEP: &nbsp;</label>"
                + "                                    <input type='text' name='cep' value='" + cep + "' class='input-medium' id='input11'>"
                + "                                </div>"
                + "                            </fieldset>"
                + "                            <hr>"
                + "                            <input class='btn btn-primary' type='submit' value='Salvar'>"
                + "                        </form>");
        out.print(rodape);
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
